"""
为了保证不跳转其他状态
锁住其他无关端口
修改于2023/2/3
"""

"""
首先读取main函数运行后留下的marky变量
读取表格数据
根据表格数据对其他无关变量锁定
运行mark.py文件
注意修改其读取对mark.txt，使其指向main文件生成的mark

此back函数无法完全准确的针对四态及六态问题有正常反馈
若命令框报错124逻辑错误，需要手动校正
"""

import mark
from links import links
import xlrd

filename = r'副本SP功能降级_仿真_V1.2_20221219.xls'
file = r'./5_4/-con2-con3.txt'
mark_file = r'./5_4/-con2-con3-con4.txt'
global marky
marky = mark.Mark()


def back(out, lock, val, i2, t):
    if marky[str(out)][0] == '0':
        if int(lock) == 1:
            marky[str(out)][0] = '1'
            if int(val) == 0:
                marky[str(out)][1] = '1'
            elif int(val) == 1:
                marky[str(out)][1] = '0'
            else:
                print(f"{i2}行存在{t}态读取错误mark{out}端口123")
    elif marky[str(out)][0] == '1':
        if int(lock) == 1:
            if int(val) == int(marky[str(out)][1]):
                print(f"{i2}行{out}端口存在逻辑错误124")
            else:
                pass


SP = xlrd.open_workbook(filename)
main_sheet = SP.sheet_by_index(1)  # 获取主工作表格
print(main_sheet)
x1 = 33  # 读取行数为x1+1,例要读取15行开始，请输入14
x2 = 66  # 结尾行不被读取，例要读取至17行，请输入17
judgment = main_sheet.col_values(6, x1 - 1, x2)  # 读取x1-x2行内容
i = x1 + 1
for judge in judgment:
    if judge == '':
        i += 1
        continue
    print(i)
    data = links(judge, i)
    data.T()
    data.readout()
    if data.excep == 0:
        if data.t == 1:
            # if marky[str(data.out)][0] == '0':
            #     if int(data.lock) == 1:
            #         marky[str(data.out)][0] = '1'
            #         if int(data.lock_val) == 0:
            #             marky[str(data.out)][1] = '1'
            #         elif int(data.lock_val) == 0:
            #             marky[str(data.out)][1] = '0'
            #         else:
            #             print(f"{i}行存在一态读取错误123")
            # elif marky[str(data.out)][0] == '1':
            #     if int(data.lock) == 1:
            #         if int(data.lock_val) == int(marky[str(data.out)][1]):
            #             print(f"{i}行{data.mark}端口存在逻辑错误")
            #         else:
            #             pass
            back(data.out, data.lock, data.lock_val, i, data.t)
        elif data.t == 2:
            back(data.out, data.lock, data.lock_val, i, data.t)
            lost = str(int(data.out) + 1)
            back(lost, data.two_lock, data.two_lock_val, i, data.t)
        elif data.t == 3:
            back(data.out, data.lock, data.lock_val, i, data.t)
            lost = str(int(data.out) + 1)
            back(lost, data.two_lock, data.two_lock_val, i, data.t)
            lost1 = str(int(data.out) + 2)
            back(lost1, data.three_lock, data.three_lock_val, i, data.t)
        else:
            print(f"{i}行存在模态读取错误mark{data.out}")
    else:
        print(f"{i}行写入失败")
    data.printf()
    i += 1
# 对Mark端口进行输出
mark_f = open(mark_file, 'w')
i0 = 1
while i0 < 208:
    s = f'mark({i0},:)\t=[{marky[str(i0)][0]},{marky[str(i0)][1]}];% {marky[str(i0)][2]}\n'
    mark_f.write(s)
    mark_f.flush()
    i0 += 1
mark_f.close()
